/* 12. 借书 */
"use strict";

const CHECK = require("../myUtil/formatCheck");
const { StrTime } = require("../myUtil/lib");
const DATEUTIL = require("../myUtil/DateUtil");
const db = require("../../coSqlite3");
const HTM = require("../myUtil/lib").html;

exports.borrowBook = function* (req, res) {
    console.log("----------------------12. 借书\n");
    let rID = req.body.rID;
    let bID = req.body.bID;
    let htm = '';

    let htm1 = HTM.divBegin + 1 + HTM.divEnd;
    if (rID == "") {
        return HTM.begin + htm1 + "该证号不存在" + HTM.end;
    }

    let htm2 = HTM.divBegin + 2 + HTM.divEnd;
    if (bID == "") {
        return HTM.begin + htm2 + "该书号不存在" + HTM.end;
    }


    try {
        let rIDExist = yield db.execSQL("SELECT count(rid) as count FROM reader WHERE rid = ?", [rID]);
        if (rIDExist[0].count == 0) {
            return HTM.begin + htm1 + "该证号不存在" + HTM.end;
        }

        let bIDExist = yield db.execSQL("SELECT count(bid) as count FROM book WHERE bid = ?", [bID]);
        if (bIDExist[0].count == 0) {
            return HTM.begin + htm2 + "该书号不存在" + HTM.end;
        }

        let bIDBorrowed = yield db.execSQL("SELECT create_time, return_time FROM process WHERE rid = ? and returned = 0", [rID]);
        if (bIDBorrowed.length != 0) {
            // let shouldReturnTime = '';
            for (let bookBorrowTime of bIDBorrowed) {
              if (
                bookBorrowTime.return_time < StrTime(new Date(), "yyyy-mm-dd")
              ) {
                htm = HTM.divBegin + 3 + HTM.divEnd;
                return HTM.begin + htm + "该读者有超期书未还" + HTM.end;
              }

              // shouldReturnTime = DATEUTIL.add30Days(bookBorrowTime);
              // if (StrTime(new Date(), 'yyyy-mm-dd') > shouldReturnTime) {
              //     htm = HTM.divBegin + 3 + HTM.divEnd;
              //     return HTM.begin + htm + "该读者有超期书未还" + HTM.end;
              // }
            }
        }

        let thisBookLeft = yield db.execSQL("SELECT count(create_time) as count  FROM process WHERE returned = 0 and bid = ? and rid = ?", [bID, rID]);
        if (thisBookLeft[0].count != 0) {
          htm = HTM.divBegin + 4 + HTM.divEnd;
          return HTM.begin + htm + "该读者已经借阅该书，且未归还" + HTM.end;
        }

        let theBooks = yield db.execSQL("SELECT btruecnt, bname FROM book WHERE bid = ?", [bID]);
        if (theBooks[0].btruecnt == 0) {
          htm = HTM.divBegin + 5 + HTM.divEnd;
          return HTM.begin + htm + "该书已经全部借出" + HTM.end;
        }
        // console.log(theBooks[0].bname+"111");

        let date = StrTime(new Date(), "yyyy-mm-dd");
        yield db.execSQL('INSERT INTO process (bid, bname, rid, create_time, return_time, returned) VALUES (?, ?, ?, ?, ?, ?)', [bID, theBooks[0].bname, rID, date,  DATEUTIL.add30Days(date), 0]);
        // 库存-1
        yield db.execSQL('UPDATE book SET btruecnt = btruecnt - 1 WHERE bid = ?', [bID]);

    } catch (e) {
        htm = HTM.divBegin + 6 + HTM.divEnd + "数据库执行错误: " + e;
        return HTM.begin + htm + HTM.end;
    }

    htm = HTM.divBegin + 0 + HTM.divEnd;
    return HTM.begin + htm + '成功' + HTM.end;
}